#include <dspSimulator/instruction/stndw.h>

bool STNDW::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void STNDW::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                              std::shared_ptr<llvm::IRBuilder<>> irBuilder) {

}

std::shared_ptr<Instruction> STNDW::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<STNDW> stndw;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<5>(data, 2, 0b11101) && checkBits<1>(data, 8, 0b1) && existStandardCondition(data)) {
            stndw = std::make_shared<STNDW>(dspInstInfo->getAddress(), baseCycle);
            stndw->creg_z = extractBits<4>(data, 28);
            stndw->y = data[7];
            if (stndw->y == 0) {
                stndw->funcUnit = FuncUnit::D1;
            } else {
                stndw->funcUnit = FuncUnit::D2;
            }
            stndw->mode = extractBits<4>(data, 9).to_ulong();
            stndw->offsetR = extractBits<5>(data, 13).to_ulong();
            stndw->cst = stndw->offsetR;
            stndw->baseR = extractBits<5>(data, 18).to_ulong();
            stndw->src1 = extractBits<4>(data, 24).to_ulong() << 1;
            stndw->s = data[1];
            stndw->p = data[0];
            auto sc = data[23];
            stndw->instFormat = InstFormat::Kind1;
            if (sc == 0) {
                stndw->instFormat = InstFormat::Kind2;
            }
        }
    }
    return stndw;
}

void STNDW::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {
    if (executePhase == ExecutePhase::E1) {
        auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
        if (instFormat == InstFormat::Kind1) {
            u32 *baseRReadPtr;
            u32 *baseRWritePtr;
            if (y == 0) {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegAName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegAName[baseR])->getDataPtr();
            } else {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegBName[baseR])->getDataPtr();
            }
            visitMemAddr = computeRealAddress(baseRReadPtr, baseRWritePtr, mode, 3, dspMachine);
        } else if (instFormat == InstFormat::Kind2) {
            u32 *baseRReadPtr;
            u32 *baseRWritePtr;
            if (y == 0) {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegAName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegAName[baseR])->getDataPtr();
            } else {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegBName[baseR])->getDataPtr();
            }
            visitMemAddr = computeRealAddress(baseRReadPtr, baseRWritePtr, mode, 0, dspMachine);
        }
        dataLo = *src1LoRegPtr;
        dataHi = *src1HiRegPtr;
    } else if (executePhase == ExecutePhase::E3) {
        auto memory = targetMachine->getMMU()->getMemory();
        memory->writeNBits<32>(visitMemAddr, {dataLo});
        memory->writeNBits<32>(visitMemAddr + 4, {dataHi});
    }
}

std::shared_ptr<DspInstruction> STNDW::clone() {
    return std::make_shared<STNDW>(*this);
}

std::string STNDW::toString() const {
    std::string res;
    if (isCompactInstruction) {

    } else {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)];
    if (instFormat == InstFormat::Kind1) {
        res += " " + getSrc1Reg64Name() + ",*" + addressModeToString();
    }
    return res;
}